<template>
  <div>
    <BasicTable
      @register="registerTable"
      @resize-column="handleResizeColumn"
      :canResize="false"
      :scroll="{ x: 'max-content' }"
    >
      <template #toolbar>
        <a-button type="error" @click="handleDelete"> 批量删除 </a-button>
        <a-button type="success" @click="handleCreate">添加车队</a-button>
        <a-button type="success" @click="handleEdit">编辑车队</a-button>
      </template>
    </BasicTable>
    <CarTeamModal1 @register="registerCarTeam" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'CarTeam',
});
</script>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { BasicTable, useTable } from '@/components/Table';
import { useModal } from '@/components/Modal';
import CarTeamModal1 from './components/CarTeamModal.vue';
import { carTeamColumns, carTeamSearchFormSchema } from './data';
import { useMessage } from '@/hooks/web/useMessage';
import { defHttp } from '@/utils/http/axios';
import { useRoute, useRouter } from 'vue-router';
const { createMessage } = useMessage();
const router = useRoute();
const [registerCarTeam, { openModal: openCarTeamModal }] = useModal();
const [registerTable, { reload, getForm, getSelectRows, getSelectRowKeys }] =
  useTable({
    //title: '列表',
    api: '/admin/car_team/listPage',
    columns: carTeamColumns,
    showIndexColumn: true,
    useSearchForm: true,
    indexColumnProps: {
      fixed: 'left',
    },
    beforeFetch: (info) => {
      debugger
      if (router.query.companyId) {
        return { ...info, companyId: router.query.companyId };
      }
      return;
    },
    formConfig: {
      labelWidth: 70,
      hideLabel: true,
      schemas: carTeamSearchFormSchema,
      baseColProps: { span: 6 },
    },
    pagination: true,
    bordered: true,
    rowKey: 'id',
    rowSelection: {
      type: 'checkbox',
    },
  });
const exportLoading = ref(false);
onMounted(() => {});
function handleCreate() {
  openCarTeamModal(true, {
    isUpdate: false,
    companyId: router.query.companyId,
  });
}
function handleEdit() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个车队进行编辑');
  } else {
    openCarTeamModal(true, {
      isUpdate: true,
      id: getSelectRows()[0].id,
    });
  }
}
async function handleDelete() {
  if (getSelectRows().length === 0) {
    createMessage.error('请选择一个车队');
  } else {
    const res = await defHttp.post({
      url: `/admin/car_team/delete`,
      data: getSelectRowKeys(),
    });
    createMessage.success("已删除");
    reload();
  }
}
function handleSuccess() {
  reload();
}
</script>
